/*
 * 创建日期 2006-6-19
 */
package com.jmcgg.freesky.common.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;

/**
 * Title: FreeSky Common Java Library <br>
 * Description: upload file using apache commons-fileupload-1.2.1 <br>
 * Copyright: Copyright (c) 2005~2009 www.afss.cn <br>
 * Company: Amei's FreeSky Studio <br>
 * 
 * @author <b>Jacky Chou</b> (AFSS [Amei's FreeSky Studio])
 * @version 1.2.1, 2008-08-05, Clean and optimize the source code. <br>
 *          1.2.0, 2008-02-23, Upgrade freesky.jar to 1.2.0 with Struts 2 <br>
 *          1.0.0, 2006-05-30, Initial Version
 */
public class UploadUtil {
	/**
	 * Log4J Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(UploadUtil.class);

	public static String ROOT_UPLOAD_DIR = "/WEB-INF/";
	public static int MAX_FILE_SIZE = 10 * 1024 * 1024;

	public UploadUtil() {
	}

	@SuppressWarnings("unchecked")
	public static int saveAllFiles(HttpServletRequest request) {
		int fileNum = 0;
		try {
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			if (!isMultipart) {
				logger.error("No multi-part files found from the request");
				return fileNum;
			}
			ServletFileUpload upload = new ServletFileUpload();
			upload.setHeaderEncoding("UTF-8");
			// Parse the request
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = iter.next();
				// String fieldName = item.getFieldName();
				String fileName = (new File(item.getName())).getName();
				File uploadedFile = new File(fileName + ROOT_UPLOAD_DIR);
				item.write(uploadedFile);
				fileNum++;
			}
		} catch (FileUploadException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return fileNum;
	}

	/**
	 * save the file to the folder, and rename it with newFileName.
	 * 
	 * @param fieldName
	 *            String the field name in the form
	 * @param newFileName
	 *            String new file name
	 * @return int
	 */
	@SuppressWarnings("unchecked")
	public static int saveFile(HttpServletRequest request, String fieldName,
			String newFileName) {
		int fileNum = 0;
		try {
			boolean isMultipart = ServletFileUpload.isMultipartContent(request);
			if (!isMultipart) {
				logger.error("No multi-part files found from the request");
				return fileNum;
			}
			ServletFileUpload upload = new ServletFileUpload();
			upload.setHeaderEncoding("UTF-8");
			// Parse the request
			List<FileItem> items = upload.parseRequest(request);
			Iterator<FileItem> iter = items.iterator();
			while (iter.hasNext()) {
				FileItem item = iter.next();
				if (fieldName.equals(item.getFieldName())) {
					File uploadedFile = new File(ROOT_UPLOAD_DIR + newFileName);
					item.write(uploadedFile);
					fileNum++;
					break;
				}
			}
		} catch (FileUploadException e) {
			logger.error(e.getMessage());
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return fileNum;
	}
}
